package com.amado.easyui2.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.amado.easyui2.constant.GlobalConstants;
import com.amado.easyui2.domain.Department;
import com.amado.easyui2.exception.ApiException;
import com.amado.easyui2.service.DepartmentService;
import com.amado.easyui2.service.StudentService;
import com.amado.easyui2.service.impl.DepartmentServiceImpl;
import com.amado.easyui2.util.JsonUtil;
import com.amado.easyui2.vo.Page;
import com.amado.easyui2.vo.Result;
import com.fasterxml.jackson.databind.ObjectMapper;

public class DepartmentServlet extends HttpServlet {
	private DepartmentService departmentService;

	ApplicationContext applicationContext;

	@Override
	public void init() throws ServletException {
		// Spring容器
		applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());
		// 通过容器获取Bean
		departmentService = applicationContext.getBean("departmentServiceImpl", DepartmentService.class);
	}

	//
	/**
	 * 
	 */
	private static ObjectMapper mapper = new ObjectMapper();
	private static final long serialVersionUID = 1L;

	@Override
	protected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
		arg0.setCharacterEncoding("UTF-8");
		arg1.setHeader("Content-type", "text/html;charset=UTF-8");
		String method = arg0.getParameter("method");
		if ("add".equals(method)) {
			try {
			String strJson = arg0.getParameter("data");
			String deleteFlag = "N";
			String createName = (String) arg0.getSession().getAttribute(GlobalConstants.SESSION_USER);
			String updateName = (String) arg0.getSession().getAttribute(GlobalConstants.SESSION_USER);
			Department userInfo = JsonUtil.jsonToBean(strJson, Department.class);
			userInfo.setDeleteFlag(deleteFlag);
			userInfo.setCreateName(createName);
			userInfo.setUpdateName(updateName);
			departmentService.add(userInfo);
			}catch (ApiException e) {
				arg1.getWriter().print(JsonUtil.beanToJson(new Result(e.getCode(),e.getMessage())));
			}
		} else if ("delete".equals(method)) {
			delete(arg0, arg1);
		} else if ("query".equals(method)) {
			query(arg0, arg1);
		} else if ("queryAll".equals(method)) {
			queryAll(arg0, arg1);
		} else if ("edit".equals(method)) {
			edit(arg0, arg1);
		} else if ("update".equals(method)) {
			update(arg0, arg1);
		}
	}

	private void delete(ServletRequest arg0, HttpServletResponse arg1) throws IOException {
		String ids = arg0.getParameter("ids");
		if (ids == null || ids.trim().equals("")) {
			// 参数检查失败
			arg1.getWriter().print(JsonUtil.beanToJson(new Result("3", "参数检查失败")));
			return;
		}
		try {
			departmentService.delete(Long.valueOf(ids));
			arg1.getWriter().print(JsonUtil.beanToJson(new Result()));
		} catch (ApiException e) {
			arg1.getWriter().print(JsonUtil.beanToJson(new Result(e.getCode(),e.getMessage())));
		} catch (Exception e) {
			arg1.getWriter().print(JsonUtil.beanToJson(new Result("2","系统异常请联系管理员")));
		}
	}

	protected void edit(HttpServletRequest arg0, HttpServletResponse arg1) throws IOException {
		Long id = Long.valueOf(arg0.getParameter("id"));
		List<Department> list = departmentService.list();
		for (Department department : list) {
			if (department.getId().equals(id)) {
				Department departmentId = department;
				PrintWriter out = arg1.getWriter();
				out.print(mapper.writeValueAsString(departmentId));
				break;
			}
		}
	}

	protected void query(HttpServletRequest arg0, HttpServletResponse resp) throws IOException {
		Page page = new Page();
		String strPage = arg0.getParameter("page");
		String strRows = arg0.getParameter("rows");
		int page1 = strPage == null ? 1 : Integer.parseInt(strPage);//
		int rows = strRows == null ? 5 : Integer.parseInt(strRows);
		page.setCurrentPage(page1);
		page.setRowSize(rows);
		Page<Department> pageList = departmentService.listAll(page);
		PrintWriter out = resp.getWriter();
		out.print(mapper.writeValueAsString(pageList));
	}

	protected void queryAll(HttpServletRequest arg0, HttpServletResponse resp) throws IOException {
		List<Department> pageList = departmentService.listAll();
		PrintWriter out = resp.getWriter();
		out.print(mapper.writeValueAsString(pageList));
	}

	protected void update(HttpServletRequest arg0, HttpServletResponse resp) {
		String strJson = arg0.getParameter("data");// 解析
		Department userInfo = JsonUtil.jsonToBean(strJson, Department.class);
		Long id = userInfo.getId();
		String departmentNum = userInfo.getDepartmentNum();
		String departmentName = userInfo.getDepartmentName();
		String createName = userInfo.getCreateName();
		String updateName = userInfo.getUpdateName();
		Department department = new Department(id, departmentNum, departmentName, createName, updateName);
		departmentService.update(department);
	}
}
